package offer;

/**
 * <p>
 * 剑指 Offer 42. 连续子数组的最大和
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/24 17:19
 */
public class Offer42 {

    public int maxSubArray(int[] nums) {
        int max = nums[0];
        int former = 0;
        int cur = nums[0];
        for (int num : nums) {
            cur = num;
            if (former > 0) {
                cur += former;
            }
            if (cur > max) {
                max = cur;
            }
            former = cur;
        }
        return max;
    }
}
